<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 站点控制器
 * @author derek <693472824@qq.com>
 */
class SiteController extends BaseController {

    /**
     * 获取站点列表
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $siteName = I('site_name');
        
        $where = array();
        
        if(!empty($siteName)){
        
            $where['site_name'] = array('like', '%'.(string)$siteName.'%');
        }
        
        $siteModel = M('Site');
        $list = $this->lists($siteModel, $where);
        
        $siteIDArray = array();
        foreach ($list as $key=>$val){
            $siteIDArray[] = $val['site_id'];
        }
        
        $orderModel = M('Orders');
        
        $map['site_id'] = array('in', $siteIDArray);
        $map['status'] = array('egt', '3');
        
        $income = $orderModel->where($map)->group('site_id')->getField('site_id,sum(turnover)');
        $total = $orderModel->where($map)->group('site_id')->getField('site_id,count(order_id)');
        
        $this->assign('site_name', $siteName);
        $this->assign('list', $list);
        $this->assign('income', $income);
        $this->assign('total', $total);
        $this->display();
    }

    /**
     * 增加或修改站点
     *
     * @param integer $admin_id
     *            用户ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
        
        $siteID = I('site_id');
    
        if (!empty($siteID)) {
    
            $siteLogic = D('Site', 'Logic');
            $siteModel = $siteLogic->getSiteByID($siteID);
    
            if (empty($siteModel)) {
                $this->error('获取数据失败', U('index'));
            } else {
                $this->assign('siteModel', $siteModel);
            }
        }
    
        $this->display();
    }
    
    /**
     * 保存站点信息
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
    
        $siteLogic = D('Site', 'Logic');
        $result = $siteLogic->saveAction();
    
        if ($result === false) {
            $this->error('操作失败' . $siteLogic->getError());
        } else {
            $this->success('操作成功', U('index'));
        }
    }

    /**
     * 站点删除
     * @author derek <693472824@qq.com>
     */
    public function del() {
        
        $siteID = I('siteids');
        
        if(empty($siteID)){
            $this->error('参数错误！');
        }
        
        $siteLogic = D('Site', 'Logic');
        $result = $siteLogic->del($siteID);
        
        if ($result === false) {
            $this->error('参数错误', U('index'));
        } else {
            $this->success('操作成功！', U('index'));
        }
    }
}